package com.guoliang.eduService.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guoliang.commonUtils.CommonResult;
import com.guoliang.eduService.entity.EduTeacher;
import com.guoliang.eduService.entity.vo.TeacherQuery;
import com.guoliang.eduService.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author leo
 * @since 2023-01-12
 */
@RestController
@RequestMapping("/eduService/teacher")
@Api(description = "讲师管理")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;
    //查询讲师所有信息
    @ApiOperation("查询所有讲师")
    @GetMapping("findAll")
    public CommonResult findAllTeacher(){
        List<EduTeacher> teachers= eduTeacherService.list();;
        return CommonResult.ok().data("item",teachers);
    }
    //删除讲师
    @DeleteMapping("{id}")
    public CommonResult removeById(@PathVariable("id")String id){
        Boolean flag=eduTeacherService.removeById(id);
        if (flag){
            return CommonResult.ok();
        }else return CommonResult.error();
    }
    //分页查询
    @GetMapping("pageTeacher/{curPage}/{limit}")
    public CommonResult findPage(@PathVariable("curPage")Integer current,@PathVariable("limit")Integer limit){
        Page<EduTeacher> pageTeacher=new Page<>(current,limit);
        eduTeacherService.page(pageTeacher, null);
        long total = pageTeacher.getTotal();
        List<EduTeacher> teachers = pageTeacher.getRecords();
        return CommonResult.ok().data("total",total).data("rows",teachers);
    }
    //条件查询加分页
    @PostMapping("pageTeacherCondition/{curPage}/{limit}")
    public CommonResult pageTeacherCondition(@PathVariable("curPage")Long current
            , @PathVariable("limit")Long limit,@RequestBody TeacherQuery teacherQuery){
        Page<EduTeacher> teacherPage=new Page<>(current,limit);
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        //构建动态sql
        if (!StringUtils.isEmpty(teacherQuery.getName())) wrapper.like("name",teacherQuery.getName() );
        if (!StringUtils.isEmpty(teacherQuery.getLevel())) wrapper.eq("level",teacherQuery.getLevel());
        if (!StringUtils.isEmpty(teacherQuery.getBegin())) wrapper.ge("gmt_create",teacherQuery.getBegin());
        if (!StringUtils.isEmpty(teacherQuery.getEnd())) wrapper.le("gmt_create",teacherQuery.getEnd());
        eduTeacherService.page(teacherPage,wrapper);
        return CommonResult.ok().data("total",teacherPage.getTotal()).data("rows",teacherPage.getRecords());
    }
    //添加讲师的接口方法
    @ApiOperation("讲师添加")
    @PostMapping("addTeacher")
    public CommonResult addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.save(eduTeacher);
        if (flag) return CommonResult.ok();
        else return CommonResult.error();
    }
    //根据讲师id查询数据
    @ApiOperation("根据id查询讲师")
    @GetMapping("getTeacher/{id}")
    public CommonResult getTeacherById(@PathVariable("id")String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return CommonResult.ok().data("teacher",teacher);
    }
    //修改讲师
    @ApiOperation("讲师修改")
    @PutMapping("updateTeacher")
    public CommonResult updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag) return CommonResult.ok();
        else return CommonResult.error();
    }
}
